package art.client.views;

import java.util.HashMap;
import java.util.Map;

import art.client.ClientFactory;
import art.client.activites.MenuHandler;
import art.shared.entities.LoginInfo;
import art.shared.entities.UserLevelShared;

import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.IsWidget;

public class MenuView extends Composite implements IsWidget {

	private final UserLevelShared level;
	private HorizontalPanel panel = new HorizontalPanel();
	private Map<String, Anchor> anchorsMap = new HashMap<String, Anchor>();

	public MenuView(ClientFactory factory, LoginInfo loginInfo) {
		super();
		if (loginInfo == null) {
			this.level = UserLevelShared.VISITOR;
		} else {
			this.level = loginInfo.getUserLevel();
		}
		initMenu();
		initWidget(panel);
		new MenuHandler(factory, this, loginInfo);
	}

	private void initMenu() {
		panel.addStyleName("menu-panel");
		switch (this.level) {
		case VISITOR:
			addAnchor("Login");
			addAnchor("Arts");
			break;
		case NORMAL:
			addAnchor("Arts");
			addAnchor("Own arts");
			addAnchor("Upload arts");
			addAnchor("Edit profile");
			addAnchor("Logout");
			break;
		case ADMIN:
			addAnchor("Arts");
			addAnchor("Own arts");
			addAnchor("Upload arts");
			addAnchor("Edit profile");
			addAnchor("Users");
			addAnchor("Logout");
			break;
		}
	}

	private void addAnchor(String name) {
		Anchor anchor = new Anchor(name);
		panel.add(anchor);
		anchorsMap.put(name, anchor);
		anchor.addStyleName("menu-link-element");
	}

	public Anchor getAnchor(String name) {
		return anchorsMap.get(name);
	}
}
